const domainName = getApp().globalData.domainName
const header = {'content-type':'application/x-www-form-urlencoded'}
const dataType = 'json'
const method = 'POST'
const responseType = 'text'



  // 修改组名名称
const update_label_name=(that,label_id,set_name)=>{
    wx.request({
      url: domainName + '/update_label_name',
      data: {
        label_id:label_id,
        set_name:set_name,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        let update_res = res.data.update_res
        if(update_res=='existed'){
          wx.showModal({
            content: '名称已存在，请重新命名',
            showCancel: false,
            title: '提醒',
          })
        }else if(update_res){
          wx.showToast({
            duration: 1500,
            icon: 'success',
          })
          load_jg_label_list(that, that.jg_id)
        }
      },
    })
  }


  // 设置组名状态
const set_label_status=(that,label_id,set_status)=>{

    wx.request({
      url: domainName + '/set_label_status',
      data: {
        label_id:label_id,
        set_status:set_status,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        // console.log('res',res.data)
        if(res.data.update_res){
          load_deleted_jgLabels(that, that.jg_id)
          load_jg_label_list(that, that.jg_id)
        }
      },
    })
    
  }

  // 加载组名列表
  const load_jg_label_list=(that,jg_id)=>{
    wx.request({
      url: domainName + '/load_jg_label_list',
      data: {
        jg_id:jg_id,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        that.setData({
          jg_label_list:res.data.jg_label_list,

          selected_index:null,
        })
      },
    })
  }

  // 加载已被删除的组名列表
  const load_deleted_jgLabels=(that,jg_id)=>{
    wx.request({
      url: domainName + '/load_deleted_jgLabels',
      data: {
        jg_id:jg_id,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        that.setData({
          deleted_jg_label_list:res.data.deleted_jg_label_list,
          selected_index:null,
          show_deleted:true,
        })
      },
    })
  }

  // 增加标签
const add_label=(that)=>{
    wx.showModal({
      editable: true,
      placeholderText: '请输入组名/批次',
      showCancel: true,
      title: '添加组名/批次',
      success: res => {
        if(res.confirm){
          let label_name = res.content
          wx.request({
            url: domainName + '/add_stu_label',
            data: {
              jg_id:that.jg_id,
              label_name:label_name,
            },
            dataType: dataType,
            enableCache: true,
            enableHttp2: true,
            enableQuic: true,
            header: header,
            method: method,
            responseType: responseType,
            timeout: 0,
            success: res => {
              let row_id = res.data.row_id
              if(row_id=='existed'){
                wx.showModal({
                  content: '名称已存在，请重新命名',
                  showCancel: false,
                  title: '提醒',
                })
              }else if(row_id){
                wx.showToast({
                  title: '添加完成!',
                  duration: 1500,
                  icon: 'success',
                })
                load_jg_label_list(that, that.jg_id)
                wx.pageScrollTo({
                  scrollTop: 0,
                })
              }
            },
          })
        }
      },
    })
  }  

module.exports={
  load_deleted_jgLabels,
  update_label_name,
  set_label_status,
  load_jg_label_list,
  add_label,
}